var express = require('express');
var router = express.Router();

const MyQuery = require('../utils/db')

const timeStamp = require('time-stamp');

//引入配置文件
const { API_URL } = require('../utils/config');

router.get('/demo3', function(req, res, next) {
    res.send('详情模块')
});

//课程详情接口，按照cid获取某一个的详情。cid把它理解为课程的唯一标识即可。
//一个课程，按照restful规范，不带s
router.get('/course', async(req, res, next) => {

    let { id = '' } = req.query

    let data = {} //最终要返回的数据格式  msg,status,result

    if (id == '') {
        data = {
            msg: '请输入id',
            status: 500
        }
        res.send(data)
        return
    }
    //1. 写sql
    let sql1 = `SELECT *,CONCAT('${API_URL}/uploads/courses/',detail_imgs) AS detail_imgs,CONCAT('${API_URL}/uploads/courses/',image_src) AS image_src FROM e_course WHERE cid = '${id}' `
        //2. 执行sql
    let { result: result1 } = await MyQuery(sql1) //{ err,result }
        //result1[0] 课程的信息
        //获取到课程信息后，里面有tid：代表这个课程是哪个老师上的。
        //而且分析前台的html页面，详情页中需要展示老师

    let sql2 = `SELECT *,CONCAT('${API_URL}/uploads/avatar/',head_photo_url) AS head_photo_url FROM e_teacher WHERE tid='${result1[0].tid}'`
    let { result: result2 } = await MyQuery(sql2) //{ err,result }  

    // res.send(result2[0]) //老师的对象信息
    // return

    //这个是课程的章节信息
    let sql3 = ` SELECT id,vnum,video_title  FROM e_video WHERE cid = '${id}'`
    let { result: result3 } = await MyQuery(sql3) //{ err,result }  

    //3. 要处理成规范、格式化的json数据
    data = {
        msg: 'OK',
        status: 200,
        result: {
            course: result1[0],
            teacher: result2[0],
            videos: result3
        }
    }
    res.send(data)
})

// //课程详情
// router.get('/course', async(req, res, next) => {
//     let { id = '' } = req.query;
//     let data = {};

//     if (id == '') {
//         data = {
//             msg: '请输入id',
//             status: 500
//         }
//         res.send(data)
//         return
//     }
//     // let sql1 = `select cou.cid as cid,cou.title,concat('${API_URL}/uploads/courses/',detail_imgs) as detail_imgs,concat('${API_URL}/uploads/courses/',image_src) as image_src,cou.type_name,cou.area_name,cou.category_name,cou.subject_name,cou.grade_name,cou.price,cou.intro,cou.tid  from e_course cou ,e_teacher tea, e_video vid
//     // where cou.tid = tea.tid and cou.cid = vid.cid and cou.cid = '${id}'`;

//     // let sql2 = `select tea.*,CONCAT('${API_URL}/uploads/avatar/',head_photo_url) AS head_photo_url  from e_course cou ,e_teacher tea, e_video vid
//     // where cou.tid = tea.tid and cou.cid = vid.cid and cou.cid = '${id}'`;
//     // let sql3 = `select vid.id,vid.vnum,vid.video_title  from e_course cou ,e_teacher tea, e_video vid
//     // where cou.tid = tea.tid and cou.cid = vid.cid and cou.cid = '${id}'`;
//     // let { result: course } = await MyQuery(sql1);
//     // let { result: teacher } = await MyQuery(sql2);
//     // let { result: videos } = await MyQuery(sql3);

//     let sql1 = `select 
//     cou.cid as cid,
//     cou.title,
//     concat('${API_URL}/uploads/courses/',detail_imgs) as detail_imgs,
//     concat('${API_URL}/uploads/courses/',image_src) as image_src,
//     cou.type_name,cou.area_name,cou.category_name,cou.subject_name,
//     cou.grade_name,cou.price,cou.intro,cou.tid  
//     from e_course cou ,e_teacher tea, e_video vid
//     where cou.tid = tea.tid and cou.cid = vid.cid and cou.cid = '${id}'`;

//     let sql2 = `select tea.*,CONCAT('${API_URL}/uploads/avatar/',head_photo_url) AS head_photo_url  from e_course cou ,e_teacher tea, e_video vid
//     where cou.tid = tea.tid and cou.cid = vid.cid and cou.cid = '${id}'`;
//     let sql3 = `select vid.id,vid.vnum,vid.video_title  from e_course cou ,e_teacher tea, e_video vid
//     where cou.tid = tea.tid and cou.cid = vid.cid and cou.cid = '${id}'`;
//     let { result: course } = await MyQuery(sql1);
//     let { result: teacher } = await MyQuery(sql2);
//     let { result: videos } = await MyQuery(sql3);


//     data = {
//         "msg": "OK",
//         "status": 200,
//         result: {
//             course: course[0],
//             teacher: teacher[0],
//             videos
//         }
//     };
//     res.send(data);

//     // data = {
//     //     "msg": "OK",
//     //     "status": 200,
//     //     result: {
//     //         course: course[0],
//     //         teacher: teacher[0],
//     //         videos
//     //     }
//     // };
//     // res.send(data);
// });



//报名课程
router.post('/study', async(req, res, next) => {

    let { mid = '', cid = '' } = req.body

    let data = {} //最终要返回的数据格式  msg,status,result

    if (mid == '' || cid == '') {
        data = {
            "msg": "请传递必要参数",
            "status": 500
        }
        res.send(data)
        return
    }

    //1. 写sql
    let sql = `SELECT * FROM e_order WHERE mid='${mid}' AND cid='${cid}' `
        //2. 执行sql有无此报名信息的sql
    let { result } = await MyQuery(sql) //{ err,result }
    if (result.length > 0) {
        data = {
            "msg": "已经报名此课程",
            "status": 500
        }
        res.send(data)
        return
    }

    let createdate = Date.now();
    let oid = timeStamp('YYYYMMDDHHmmss') + (Math.random() + '').substring(2, 8) //订单号，是唯一的。年月日时分秒+6位随机数
    let sql2 = ` INSERT INTO e_order ( oid,cid,mid,createdate )  VALUES ( '${oid}','${cid}','${mid}',${createdate} )`
    let { result: result2 } = await MyQuery(sql2) //{ err,result }

    //result2.affectedRows>0
    //3. 要处理成规范、格式化的json数据
    console.log(result2);
    if (result2.affectedRows > 0) {
        data = {
            msg: 'OK',
            status: 200
        }
        res.send(data)
    } else {
        res.send(data);
    }

})


//获取视频列表的接口
router.get('/video', async(req, res, next) => {

    let { id = '' } = req.query

    let data = {} //最终要返回的数据格式  msg,status,result
    if (id == '') {
        data = {
            msg: '请输入id',
            status: 500
        }
        res.send(data)
        return
    }
    //1. 写sql
    // let sql = `SELECT * FROM e_video WHERE id = ${id} `

    let sql = `SELECT video_title,CONCAT('${API_URL}/uploads/videos/',video_src) AS video_src FROM e_video WHERE id='${id}'`
        //2. 执行sql
    let { result } = await MyQuery(sql) //{ err,result }

    //3. 要处理成规范、格式化的json数据
    data = {
        msg: 'OK',
        status: 200,
        result: result[0]
    }
    res.send(data)
})



module.exports = router;